package template;

import java.sql.SQLException;

import template.PageLayout;

import net.databinder.auth.AuthSession;
import net.databinder.auth.components.ao.DataUserStatusPanel;
import net.java.ao.DatabaseProvider;
import net.java.ao.EntityManager;

import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;

/** Pages that requires you to be a logged in user to access. */
//used as a superclass to all pages requiring login
//provides basic page layout and protected variables to obtain session/layout
@AuthorizeInstantiation(Roles.USER)
public class Restricted extends WebPage {
	
	//protected variables for session/layout for subclasses to reference
	protected PageLayout _layout;
	protected AuthSession _session;
	
	//private class that checks whether the user has their default calendars
	private class CalendarChecker {
		private Person _user;
		public CalendarChecker(Person user) {
			_user = user;
		}
		
		//checks for and creates 3 default calendars for the user
		//1) Editable Events, events that the user can edit shared from other users
		//2) Viewable Events, events they can look at shared from other users
		//3) Default Calendar, the default calendar for the user
		public void checkAndCreate() {
			EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
					"jdbc:hsqldb:mem:basic-ao", null, null));
			Calendar[] calendars = _user.getCalendars();
			if(calendars.length < 3) {
				Calendar defaultCal, editableCal, viewableCal;
				defaultCal = editableCal = viewableCal = null;
				try {
					editableCal = manager.create(Calendar.class);
					viewableCal = manager.create(Calendar.class);
					defaultCal = manager.create(Calendar.class);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				editableCal.setName("Editable Events");
				editableCal.setCreator(_user);
				editableCal.save();
				
				viewableCal.setName("Viewable Events");
				viewableCal.setCreator(_user);
				viewableCal.save();
				
				defaultCal.setName("Default Calendar");
				defaultCal.setCreator(_user);
				defaultCal.save();
			}
		}
	}
	//sets protected variables and adds links to each of the specific pages to the layout
	public Restricted() {
		
	    _layout = new PageLayout("calendarBorder");
	    _session = (AuthSession)_layout.getSession();
	    CalendarChecker checker = new CalendarChecker((Person)_session.getUser());
		checker.checkAndCreate();
	    add(_layout);
	    _layout.add(new DataUserStatusPanel("user-status"));
	    
	    // navigation for auth-ed users
	    _layout.add(new BookmarkablePageLink("createCalendar", CreateCalendar.class));
	    _layout.add(new BookmarkablePageLink("createEvent", CreateEvent.class));
	    _layout.add(new BookmarkablePageLink("viewCalendars", ViewCalendars.class));
	    _layout.add(new BookmarkablePageLink("shareCalendar", ShareCalendar.class));
	    _layout.add(new BookmarkablePageLink("editCalendar", EditCalendar.class));
	    _layout.add(new BookmarkablePageLink("editEvent", EditEvent.class));
	    _layout.add(new BookmarkablePageLink("shareEvent", ShareEvent.class));
	}
}
